﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CSSAdriverapp
{
    /// <summary>
    /// Interaction logic for EditVehicleType.xaml
    /// </summary>
    public partial class EditVehicleType : Window
    {
        public EditVehicleType()
        {
            InitializeComponent();
            populateGrid();
        }

        public void populateGrid()
        {
            try
            {
                DataClassesDataContext db = new DataClassesDataContext();
                IQueryable<VehicleType> types = from c in db.VehicleTypes select c;
                types.ToList();
                displayDataGrid.ItemsSource = types;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        public List<VehicleType> types
        {
            get;
            set;
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            successLabel.Visibility = System.Windows.Visibility.Hidden;
            if (displayDataGrid.SelectedItem != null)
            {
                nameTextBox.Text = ((VehicleType)(displayDataGrid.SelectedItem)).Name;
                slider.Value = ((VehicleType)(displayDataGrid.SelectedItem)).NumOfSeats;               
            }
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            if (displayDataGrid.SelectedItem != null)
            {
                DataClassesDataContext db = new DataClassesDataContext();
                // Query the database for the row to be updated.
                var query =
                    from c in db.VehicleTypes
                    where c.ID == ((VehicleType)(displayDataGrid.SelectedItem)).ID
                    select c;

                // Execute the query, and change the column values
                // you want to change.
                foreach (VehicleType c in query)
                {
                    c.Name = nameTextBox.Text;
                    c.NumOfSeats = (int)slider.Value;

                    // Insert any additional changes to column values.
                }

                // Submit the changes to the database.
                try
                {
                    db.SubmitChanges();
                    successLabel.Visibility = System.Windows.Visibility.Visible;
                }
                catch (Exception c)
                {
                    Console.WriteLine(c);
                    // Provide for exceptions.
                }

                populateGrid(); //Shows new values in grid
            }
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (displayDataGrid.SelectedItem != null)
            {
                DataClassesDataContext db = new DataClassesDataContext();
                // Query the database for the rows to be deleted.
                var deleteVA =
                    from details in db.VehicleTypes
                    where details.ID == ((VehicleType)(displayDataGrid.SelectedItem)).ID
                    select details;

                foreach (var detail in deleteVA)
                {
                    db.VehicleTypes.DeleteOnSubmit(detail);
                }

                try
                {
                    db.SubmitChanges();
                    populateGrid();
                }
                catch (Exception c)
                {
                    Console.WriteLine(c);
                    // Provide for exceptions.
                }
            }
        }

    }
}
